package com.whl.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whl.GlobalProperites;
import com.whl.common.util.MD5Util;
import com.whl.project.entity.UserEntity;
import com.whl.project.mapper.UserMapper;
import com.whl.project.model.UserInfoResult;
import com.whl.project.model.UserPasswordCheckParam;

@Service
public class UserService {

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private GlobalProperites globalProperites;

	public UserInfoResult loadByAccount(String account) {
		UserEntity userEntity = userMapper.selectByAccount(account);
		if (userEntity == null) {
			return null;
		}
		UserInfoResult result = new UserInfoResult();
		result.setId(userEntity.getId());
		result.setUsername(userEntity.getUsername());
		result.setIdcd(userEntity.getIdcd());
		result.setMobile(userEntity.getMobile());
		result.setEmail(userEntity.getEmail());
		result.setNickname(userEntity.getNickname());
		result.setAddr(userEntity.getAddr());
		result.setSex(userEntity.getSex());
		return result;
	}

	public Boolean checkPassword(UserPasswordCheckParam param) {
		UserEntity userEntity = userMapper.selectByPrimaryKey(param.getUserId());
		if (userEntity == null) {
			return false;
		}
		String dbPassword = userEntity.getPassword();
		String validPassword = MD5Util.MD5(MD5Util.MD5(param.getPassword()) + globalProperites.passwordSalt());
		return validPassword.equalsIgnoreCase(dbPassword);
	}

}
